package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.EsignCompanyBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EsignCompanyMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EsignCompanyCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EsignCompanyModifyDto;
import com.molichuxing.services.infrastructure.dto.response.EsignCompanyDto;
import com.molichuxing.services.infrastructure.service.EsignCompanyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 公司印章 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-10-08
 */
@Service("esignCompanyService")
public class EsignCompanyServiceImpl implements EsignCompanyService {
    @Autowired
    EsignCompanyMapper esignCompanyMapper;

    /**
     * 添加
     *
     * @param esignCompanyCreateDto
     * @return
     * @throws Exception
     */
    @Override
    public Integer createEsignCompany(EsignCompanyCreateDto esignCompanyCreateDto) throws Exception {
        if (null == esignCompanyCreateDto) {
            throw new BizException("公司印章生成失败");
        }

        EsignCompanyBean esignCompanyBean = Convert.toEsignCompanyBean(esignCompanyCreateDto);
        esignCompanyMapper.create(esignCompanyBean);
        return esignCompanyBean.getId();
    }

    /**
     * 修改
     *
     * @param esignCompanyModifyDto
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyEsignCompany(EsignCompanyModifyDto esignCompanyModifyDto) throws Exception {
        if (null == esignCompanyModifyDto) {
            throw new BizException("公司印章生成失败");
        }

        Integer companyId = esignCompanyModifyDto.getCompanyId();
        String fullName = esignCompanyModifyDto.getFullName();
        Integer type = esignCompanyModifyDto.getType();
        if (StringUtils.isBlank(fullName) || null == companyId || null == type) {
            throw new BizException("公司印章生成失败");
        }

        EsignCompanyBean esignCompanyBean = esignCompanyMapper.getByCompanyId(companyId, type);
        if (null == esignCompanyBean) {
            throw new BizException("公司印章生成失败");
        }

        Convert.toEsignCompanyBean(esignCompanyModifyDto, esignCompanyBean);
        return (esignCompanyMapper.modifyById(esignCompanyBean) > 0) ? true : false;
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public EsignCompanyDto getEsignCompanyById(Integer id) {
        EsignCompanyBean esignCompanyBean = esignCompanyMapper.getById(id);
        return Convert.toEsignCompanyDto(esignCompanyBean);
    }

    /**
     * 通过公司id查询
     *
     * @param companyId
     * @param type
     * @return
     */
    @Override
    public EsignCompanyDto getEsignCompanyByCompanyId(Integer companyId, Integer type) {
        if (null == companyId || null == type) {
            return null;
        }

        EsignCompanyBean esignCompanyBean = esignCompanyMapper.getByCompanyId(companyId, type);
        if (null == esignCompanyBean) {
            return null;
        }
        return Convert.toEsignCompanyDto(esignCompanyBean);
    }

    /**
     * 获取印章list
     *
     * @return
     */
    @Override
    public List<EsignCompanyDto> getEsignCompanyList() {
        List<EsignCompanyBean> list = esignCompanyMapper.getList();
        return Convert.toEsignCompanyDtoList(list);
    }

    /**
     * 获取印章map
     *
     * @return
     */
    @Override
    public Map<Integer, EsignCompanyDto> getEsignCompanyMap() {
        List<EsignCompanyDto> esignCompanyList = getEsignCompanyList();
        if (null == esignCompanyList || esignCompanyList.size() < 1) {
            return null;
        }

        return esignCompanyList.stream().collect(Collectors.toMap(EsignCompanyDto::getCompanyId, a -> a, (k1, k2) -> k1));
    }


}
